import Clipboard from 'clipboard';

const Exports = {

  formatDate: function (date) {
    if (date) {
      let time = new Date(date);
      let month = time.getMonth() < 9 ? '0' + (time.getMonth() + 1) : time.getMonth() + 1;
      let day = time.getDate() < 10 ? '0' + time.getDate() : time.getDate();
      let hours = time.getHours() < 10 ? '0' + time.getHours() : time.getHours();
      let minutes = time.getMinutes() < 10 ? '0' + time.getMinutes() : time.getMinutes();
      let seconds = time.getSeconds() < 10 ? '0' + time.getSeconds() : time.getSeconds();
      return time.getFullYear() + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds;
    } else {
      return '';
    }
  },
  formatyyyyMMddDate: function (date) {
    if (date) {
      let time = new Date(date);
      let month = time.getMonth() < 9 ? '0' + (time.getMonth() + 1) : time.getMonth() + 1;
      let day = time.getDate() < 10 ? '0' + time.getDate() : time.getDate();
      return time.getFullYear() + "-" + month + "-" + day;
    } else {
      return '';
    }
  },
  copyText: function (_taht, cls) {
    var clipboard = new Clipboard(cls);
    clipboard.on('success', e => {
      _taht.$toast.success('复制成功');
      // 释放内存
      clipboard.destroy();
    })
    clipboard.on('error', e => {
      // 不支持复制
      _taht.$toast.error('该浏览器不支持自动复制');
      // 释放内存
      clipboard.destroy()
    })
  }
}

export default Exports;
